home *** CD-ROM | disk | FTP | other *** search
- /* ARC - Archive utility - ARCUSQ
-
- $define(tag,$$segment(@1,$$index(@1,=)+1))#
- $define(version,Version $tag(
- TED_VERSION DB =3.04), created on $tag(
- TED_DATE DB =06/25/85) at $tag(
- TED_TIME DB =12:11:20))#
- $undefine(tag)#
- $version
-
- (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
-
- By: Thom Henderson
-
- Description:
- This file contains the routines used to expand a file
- which was packed using Huffman squeezing.
-
- Most of this code is taken from an USQ program by Richard
- Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
-
- Language:
- Computer Innovations Optimizing C86
- */
- #include <stdio.h>
- #include "arc.h"
-
- /* stuff for Huffman unsqueezing */
-
- #define ERROR (-1)
-
- #define SPEOF 256 /* special endfile token */
- #define NUMVALS 257 /* 256 data values plus SPEOF */
-
- static struct nd /* decoding tree */
- { int child[2]; /* left, right */
- } node[NUMVALS-1];
-
- static int bpos; /* last bit position read */
- static int curin; /* last byte value read */
- static int numnodes; /* number of nodes in decode tree */
-
- init_usq(f) /* initialize Huffman unsqueezing */
- FILE *f; /* file containing squeezed data */
- {
- int i; /* node index */
-
- bpos = 99; /* force initial read */
-
- fread(&numnodes,sizeof(numnodes),1,f);
-
- if(numnodes<0 || numnodes>=NUMVALS)
- abort("File has an invalid decode tree");
-
- /* initialize for possible empty tree (SPEOF only) */
-
- node[0].child[0] = -(SPEOF + 1);
- node[0].child[1] = -(SPEOF + 1);
-
- for(i=0; i<numnodes; ++i) /* get decoding tree from file */
- { fread(&node[i].child[0],sizeof(node[i].child[0]),1,f);
- fread(&node[i].child[1],sizeof(node[i].child[1]),1,f);
- }
- }
-
- int getc_usq(f) /* get byte from squeezed file */
- FILE *f; /* file containing squeezed data */
- {
- int i; /* tree index */
-
- /* follow bit stream in tree to a leaf */
-
- for(i=0; i>=0; ) /* work down(up?) from root */
- { if(++bpos>7)
- { if((curin=getc_unp(f)) == ERROR)
- return(ERROR);
- bpos = 0;
-
- /* move a level deeper in tree */
- i = node[i].child[1&curin];
- }
- else i = node[i].child[1 & (curin >>= 1)];
- }
-
- /* decode fake node index to original data value */
-
- i = -(i + 1);
-
- /* decode special endfile token to normal EOF */
-
- i = (i==SPEOF) ? EOF : i;
- return i;
- }
-